//---------------------------------------------------------------------
// This file is part of the Microsoft BizTalk Server 2006 SDK
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// This source code is intended only as a supplement to Microsoft BizTalk
// Server 2006 release and/or on-line documentation. See these other
// materials for detailed information regarding Microsoft code samples.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
// PURPOSE.
//---------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace MsPartners.Unisys.BizTalkAdapters.AdapterWizard
{
	public class WzPageCodeProperties : Microsoft.BizTalk.Wizard.WizardInteriorPage, IWizardControlInterface
	{
		private const string namespaceRegEx = @"^[_a-zA-Z][._a-zA-Z0-9]*$";
		public event AddWizardResultEvent addWizardResultEvent;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox txtCodeNamespace;
		private System.Windows.Forms.ErrorProvider ErrProv;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.RadioButton chcCodeGenCS;
		private System.Windows.Forms.RadioButton chcCodeGenVB;
		private System.ComponentModel.IContainer components = null;

		public WzPageCodeProperties()
		{
			// This call is required by the Windows Form Designer.
			InitializeComponent();

			// TODO: Add any initialization after the InitializeComponent call
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		protected void AddWizardResult(string strName,string strValue)
		{
			PropertyPairEvent PropertyPair = new PropertyPairEvent(strName,strValue);
			OnAddWizardResult(PropertyPair);
		}

		// The protected OnRaiseProperty method raises the event by invoking 
		// the delegates. The sender is always this, the current instance 
		// of the class.
		protected virtual void OnAddWizardResult(PropertyPairEvent e)
		{
			if (e != null) 
			{
				// Invokes the delegates. 
				addWizardResultEvent(this,e);
			}
		}

		private bool GetAllStates()
		{
			return Regex.IsMatch(txtCodeNamespace.Text,namespaceRegEx);
		}

		public bool NextButtonEnabled
		{
			get {	return GetAllStates();	}
		}

		public bool NeedSummary
		{
			get {	return false;	}
		}

		private void txtCodeNamespace_Validating(object sender, System.ComponentModel.CancelEventArgs e)
		{
			if (!Regex.IsMatch(txtCodeNamespace.Text,namespaceRegEx) && txtCodeNamespace.Text.Length > 0)
			{
				ErrProv.SetError(txtCodeNamespace,
					"Please enter a valid code namespace");
			}
			else
			{
				ErrProv.SetError(txtCodeNamespace,"");
				EnableNext(GetAllStates());	
			}		
		}

		private void WzPageCodeProperties_Leave(object sender, System.EventArgs e)
		{
			AddWizardResult(WizardValues.NewProjectNamespace,txtCodeNamespace.Text);
			AddWizardResult(WizardValues.GeneratedLanguage,chcCodeGenCS.Checked.ToString());
		}

		
		#region Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(WzPageCodeProperties));
			this.label1 = new System.Windows.Forms.Label();
			this.txtCodeNamespace = new System.Windows.Forms.TextBox();
			this.ErrProv = new System.Windows.Forms.ErrorProvider(this.components);
			this.label2 = new System.Windows.Forms.Label();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.chcCodeGenVB = new System.Windows.Forms.RadioButton();
			this.chcCodeGenCS = new System.Windows.Forms.RadioButton();
			this.panelHeader.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.ErrProv)).BeginInit();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// labelTitle
			// 
			resources.ApplyResources(this.labelTitle, "labelTitle");
			// 
			// labelSubTitle
			// 
			resources.ApplyResources(this.labelSubTitle, "labelSubTitle");
			// 
			// label1
			// 
			resources.ApplyResources(this.label1, "label1");
			this.label1.Name = "label1";
			// 
			// txtCodeNamespace
			// 
			resources.ApplyResources(this.txtCodeNamespace, "txtCodeNamespace");
			this.txtCodeNamespace.Name = "txtCodeNamespace";
			this.txtCodeNamespace.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtCodeNamespace_KeyPress);
			this.txtCodeNamespace.Validating += new System.ComponentModel.CancelEventHandler(this.txtCodeNamespace_Validating);
			// 
			// ErrProv
			// 
			this.ErrProv.ContainerControl = this;
			resources.ApplyResources(this.ErrProv, "ErrProv");
			// 
			// label2
			// 
			resources.ApplyResources(this.label2, "label2");
			this.label2.Name = "label2";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.chcCodeGenVB);
			this.groupBox1.Controls.Add(this.chcCodeGenCS);
			resources.ApplyResources(this.groupBox1, "groupBox1");
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.TabStop = false;
			// 
			// chcCodeGenVB
			// 
			resources.ApplyResources(this.chcCodeGenVB, "chcCodeGenVB");
			this.chcCodeGenVB.Name = "chcCodeGenVB";
			// 
			// chcCodeGenCS
			// 
			this.chcCodeGenCS.Checked = true;
			resources.ApplyResources(this.chcCodeGenCS, "chcCodeGenCS");
			this.chcCodeGenCS.Name = "chcCodeGenCS";
			this.chcCodeGenCS.TabStop = true;
			// 
			// WzPageCodeProperties
			// 
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.txtCodeNamespace);
			this.Name = "WzPageCodeProperties";
			this.SubTitle = "Specify code namespace";
			this.Title = "Code properties";
			this.Load += new System.EventHandler(this.WzPageCodeProperties_Load);
			this.Leave += new System.EventHandler(this.WzPageCodeProperties_Leave);
			this.Controls.SetChildIndex(this.panelHeader, 0);
			this.Controls.SetChildIndex(this.txtCodeNamespace, 0);
			this.Controls.SetChildIndex(this.label1, 0);
			this.Controls.SetChildIndex(this.label2, 0);
			this.Controls.SetChildIndex(this.groupBox1, 0);
			this.panelHeader.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.ErrProv)).EndInit();
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);
			this.PerformLayout();

		}
		#endregion

		private void txtCodeNamespace_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if (Regex.IsMatch(txtCodeNamespace.Text,namespaceRegEx) && txtCodeNamespace.Text.Length > 0)
			{
				EnableNext(true);	
			}		
		}

		private void WzPageCodeProperties_Load(object sender, EventArgs e)
		{
#if DEBUG
			txtCodeNamespace.Text = "Unisys";
			chcCodeGenCS.Select();
#endif
		}

	}
}

